﻿using Pdelvo.Minecraft.Network;

namespace Pdelvo.Minecraft.Protocol.Packets
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    [RequireVersion(27, throwException: true)]
    public class MapChunk : Packet
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MapChunk"/> class.
        /// </summary>
        /// <remarks></remarks>
        public MapChunk()
        {
            Code = 0x33;
        }

        /// <summary>
        /// Gets or sets the X.
        /// </summary>
        /// <value>The X.</value>
        /// <remarks></remarks>
        public int X { get; set; }
        /// <summary>
        /// Gets or sets the Z.
        /// </summary>
        /// <value>The Z.</value>
        /// <remarks></remarks>
        public int Z { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [ground up contiguous].
        /// </summary>
        /// <value><c>true</c> if [ground up contiguous]; otherwise, <c>false</c>.</value>
        /// <remarks></remarks>
        public bool GroundUpContiguous { get; set; }
        /// <summary>
        /// Gets or sets the primary bitmap.
        /// </summary>
        /// <value>The primary bitmap.</value>
        /// <remarks></remarks>
        public short PrimaryBitmap { get; set; }
        /// <summary>
        /// Gets or sets the add bitmap.
        /// </summary>
        /// <value>The add bitmap.</value>
        /// <remarks></remarks>
        public short AddBitmap { get; set; }

        /// <summary>
        /// Gets or sets the size of the compressed.
        /// </summary>
        /// <value>The size of the compressed.</value>
        /// <remarks></remarks>
        public int CompressedSize { get; set; }
        /// <summary>
        /// Gets or sets the unused int.
        /// </summary>
        /// <value>The unused int.</value>
        /// <remarks></remarks>
        public int UnusedInt { get; set; }
        /// <summary>
        /// Gets or sets the compressed data.
        /// </summary>
        /// <value>The compressed data.</value>
        /// <remarks></remarks>
        public byte[] CompressedData { get; set; }

        /// <summary>
        /// Receives the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        public override void Receive(BigEndianStream reader, int version)
        {
            X = reader.ReadInt();
            Z = reader.ReadInt();

            GroundUpContiguous = reader.ReadBool();
            PrimaryBitmap = reader.ReadShort();
            AddBitmap = reader.ReadShort();

            CompressedSize = reader.ReadInt();
            UnusedInt = reader.ReadInt();
            CompressedData = reader.ReadBytes(CompressedSize);
        }

        /// <summary>
        /// Sends the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        protected override void Send(BigEndianStream writer, int version)
        {
            writer.Write(Code);

            writer.Write(X);
            writer.Write(Z);

            writer.Write(GroundUpContiguous);
            writer.Write(PrimaryBitmap);
            writer.Write(AddBitmap);

            writer.Write(CompressedSize);
            writer.Write(UnusedInt);
            writer.Write(CompressedData);
        }
    }
}